<template>
	<a-divider class="divider-top"/>
	<!--	表单头部		-->
	<a-form :model="props.submitFormData.header" ref="formRef" :label-col="{ span: 6 }" :wrapper-col="{ span: 14 }" :rules="headerRules">
		<a-row>
			<a-col :span="8">
				<a-form-item label="单据编号：" name="billOrderNo">
					<a-input  disabled v-model:value="props.submitFormData.header.billOrderNo" placeholder="单据编号" />
				</a-form-item>
			</a-col>
			<a-col :span="8">
				<a-form-item label="原始单号：" name="oldOrderNo">
					<a-input  v-model:value="props.submitFormData.header.oldOrderNo" placeholder="请输入原始单号" />
				</a-form-item>
			</a-col>
			<a-col :span="8">
				<a-form-item :label="props.type === 'bill' ? '单据日期：' : '交货日期'" name="billDate">
					<a-date-picker
						value-format="YYYY-MM-DD"
						v-model:value="props.submitFormData.header.billDate" placeholder="请选择日期" />
				</a-form-item>
			</a-col>
		</a-row>
		<a-row >

			<a-col :span="8">
				<a-form-item label="客户：" name="customerIds">
					<a-select v-model:value="props.submitFormData.header.customerIds"
							  :field-names="{label: 'customerName', value: 'customerId'}"
							  :options="customerData" placeholder="请选择客户">
					</a-select>
				</a-form-item>
			</a-col>
<!--			<a-col :span="8" >-->
<!--				<a-form-item label="出库仓库：" name="warehousingIds">-->
<!--					<a-tree-select-->

<!--						v-model:value="props.submitFormData.header.warehousingIds"-->
<!--						show-search-->
<!--						style="width: 100%"-->
<!--						:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"-->
<!--						placeholder="请选择出库仓库"-->
<!--						allow-clear-->
<!--						tree-default-expand-all-->
<!--						:tree-data="wareTree"-->
<!--						:field-names="{-->
<!--							  children: 'children',-->
<!--							  label: 'wareName',-->
<!--							  value: 'wareId',-->
<!--							}"-->
<!--						tree-node-filter-prop="wareName"-->
<!--					></a-tree-select>-->
<!--				</a-form-item>-->
<!--			</a-col>-->
			<a-col :span="8">
				<a-form-item label="发票类型：" name="invoiceType">
					<a-select  v-model:value="props.submitFormData.header.invoiceType" :options="invoiceTypeOptions" placeholder="请选择发票类型">
					</a-select>
				</a-form-item>
			</a-col>
			<a-col :span="8">
				<a-form-item label="业务员：" name="userIds">
					<xn-user-selector
						:org-tree-api="selectorApiFunction.orgTreeApi"
						:user-page-api="selectorApiFunction.userPageApi"
						:user-list-by-id-list-api="selectorApiFunction.userListByIdListApi"
						data-type="object"
						:add-show="true"
						:radio-model="true"
						v-model:value="props.submitFormData.header.userIds"
					/>
				</a-form-item>
			</a-col>

		</a-row>
		<a-row >
			<!--					<a-col :span="8">-->
			<!--						<a-form-item label="定金：" name="deposit">-->
			<!--							<a-input class="record-header" v-model:value="props.submitFormData.header.deposit" placeholder="请输入定金" />-->
			<!--						</a-form-item>-->
			<!--					</a-col>-->
			<!--					<a-col :span="8">-->
			<!--						<a-form-item label="总金额：" name="orderAmount">-->
			<!--							<a-input class="record-header" disabled v-model:value="props.submitFormData.header.orderAmount" placeholder="总金额" />-->
			<!--						</a-form-item>-->
			<!--					</a-col>-->


			<a-col :span="8">
				<a-form-item label="备注：" name="remark">
					<a-textarea class="record-header" v-model:value="props.submitFormData.header.remark" placeholder="请输入备注" />
				</a-form-item>
			</a-col>
		</a-row>
	</a-form>


</template>
<script setup >
import XnUserSelector from "@/components/XnUserSelector/index.vue";
import tool from "@/utils/tool";
import imsSupplierApi from "@/api/ims/imsSupplierApi";
import {ref} from "vue";
import imsWarehouseApi from "@/api/ims/imsWarehouseApi";
import imsOrderApi from "@/api/ims/imsOrderApi";
import bizOrgApi from "@/api/biz/bizOrgApi";
import userCenterApi from "@/api/sys/userCenterApi";
import imsCustomerApi from "@/api/ims/imsCustomerApi";
const props = defineProps({
	submitFormData: {
		type: Object,
		default: () => ({
			header: {
				userIds: [{}],
				invoiceType: '不开票',
			},
			recordList: [],
		}),
	},
	typeName: {
		type: String,
		default: () => ''
	},
	type: {
		type: String,
		default: () => ''
	}
});

// 校验
const validate = () => {
	return formRef.value.validate();
};

const supplierData = ref([])
const wareTree = ref([])
const invoiceTypeOptions = ref([])
const customerData = ref([])
const formRef = ref(null)

const headerRules = ref({
	billOrderNo: [{ required: true, message: '单据编号不能为空', trigger: ['blur', 'input'] }],
	// billDate: [{ required: true, message: '单据日期不能为空', trigger: ['blur', 'change'] }],
	invoiceType: [{ required: true, message: '发票类型为必选项', trigger: ['blur', 'input'] }],
	supplierIds: [{ required: true, message: '供应商为必选项', trigger: ['blur', 'input'] }]
})
const selectorApiFunction = {
	orgTreeApi: (param) => {
		return bizOrgApi.orgTreeSelector(param).then((data) => {
			return Promise.resolve(data)
		})
	},
	userPageApi: (param) => {
		return bizOrgApi.orgUserSelector(param).then((data) => {
			return Promise.resolve(data)
		})
	},
	userListByIdListApi: (param) => {
		console.log('查询参数：', param, !param.idList)
		if (!param.idList) {
			return false
		}
		return userCenterApi.userCenterGetUserListByIdList(param).then((data) => {
			return Promise.resolve(data)
		})
	}
}

// 获取当前用户
const uesrInfo = tool.data.get('USER_INFO')
props.submitFormData.header.userIds[0] = uesrInfo.id
onMounted(() => {



	// 生成单据编号
	imsOrderApi.getBillNo(props.typeName).then(res => {
		props.submitFormData.header.billOrderNo = res
		console.log('获取的单据编号：', res )
	})

	// 获取发票类型，默认选中不开票
	invoiceTypeOptions.value = tool.dictList('fill_type')


	// 获取客户/客户
	imsCustomerApi.imsCustomerPage({
		current: 1,
		size: 9999
	}).then(res => {
		// 客户名称 = 客户编号+客户名称
		customerData.value = res.records
		console.log('获取的客户：', res.records )
	})


	// 单据日期获取当前时间
	function currentDate() {
		const now = new Date();
		const year = now.getFullYear();
		const month = (now.getMonth() + 1).toString().padStart(2, '0');
		const day = now.getDate().toString().padStart(2, '0');
		return `${year}-${month}-${day}`;
	}

	console.log('获取当前日期', currentDate())
	props.submitFormData.header.billDate = currentDate()
	// 获取供应商/客户
	imsSupplierApi.imsSupplierPage({
		current: 1,
		size: 9999
	}).then(res => {
		// 供应商名称 = 供应商编号+供应商名称
		supplierData.value = res.records
		console.log('获取的供应商：', res.records )
	})
	// 获取仓库
	imsWarehouseApi.wareTree().then(res => {
		wareTree.value = res
		console.log('获取的仓库：', res )
	})




})


// 暴露 validate 方法
defineExpose({ validate });
</script>
